<template>
  <div class="game-page">
    <!-- 顶部横幅 -->
    <div class="banner">
      <div class="banner-content">
        <h1>海洋环保知识问答</h1>
        <p>测试你的海洋环保知识，赢取积分奖励</p>
      </div>
    </div>

    <!-- 游戏区域 -->
    <div class="game-container">
      <!-- 开始界面 -->
      <el-card v-if="!gameStarted" class="start-card">
        <div class="game-intro">
          <div class="quiz-icon">
            <el-icon :size="80" color="#409EFF"><QuestionFilled /></el-icon>
          </div>
          <h2>准备好开始了吗？</h2>
          <p>共{{ totalQuestions }}道题目，答对可获得积分奖励</p>
          <div class="rules">
            <h3>游戏规则：</h3>
            <ul>
              <li>每道题有30秒答题时间</li>
              <li>答对一题可获得10积分</li>
              <li>连续答对可获得额外积分</li>
              <li>答错不扣分，但会重置连续答对次数</li>
            </ul>
          </div>
          <el-button type="primary" size="large" @click="startGame" round>
            开始答题
          </el-button>
        </div>
      </el-card>

      <!-- 答题界面 -->
      <el-card v-else-if="!gameFinished" class="question-card">
        <div class="progress-info">
          <div class="question-progress">
            <span class="progress-text">第 {{ currentQuestionIndex + 1 }}/{{ totalQuestions }} 题</span>
            <span class="combo-text" v-if="combo > 1">连击 x{{ combo }}</span>
          </div>
          <div class="timer-container">
            <el-progress 
              type="circle" 
              :percentage="timePercentage"
              :status="timePercentage <= 20 ? 'exception' : ''"
              :width="60"
              :stroke-width="6"
            >
              <template #default>
                <span class="time-text">{{ Math.ceil(timePercentage * 0.3) }}s</span>
              </template>
            </el-progress>
          </div>
          <div class="score">
            得分：{{ score }}
          </div>
        </div>

        <div class="question-content">
          <h3>{{ currentQuestion.question }}</h3>
          <div class="options">
            <el-button
              v-for="(option, index) in currentQuestion.options"
              :key="index"
              :class="[
                'option-button',
                {
                  'correct': showAnswer && index === currentQuestion.correctAnswer,
                  'wrong': showAnswer && selectedAnswer === index && index !== currentQuestion.correctAnswer
                }
              ]"
              :disabled="showAnswer"
              @click="selectAnswer(index)"
            >
              <div class="option-wrapper">
                <span class="option-index">{{ ['A', 'B', 'C', 'D'][index] }}</span>
                <span class="option-text">{{ option }}</span>
              </div>
            </el-button>
          </div>
        </div>

        <div class="feedback" v-if="showAnswer">
          <div :class="['feedback-content', isCorrect ? 'correct' : 'wrong']">
            <el-icon :size="24">
              <component :is="isCorrect ? 'CircleCheckFilled' : 'CircleCloseFilled'" />
            </el-icon>
            <span>{{ isCorrect ? '回答正确！' : '回答错误' }}</span>
          </div>
          <div class="explanation">
            {{ currentQuestion.explanation }}
          </div>
        </div>
      </el-card>

      <!-- 结果界面 -->
      <el-card v-else class="result-card">
        <div class="game-result">
          <h2>答题完成！</h2>
          <div class="result-stats">
            <div class="stat-item">
              <div class="stat-value">{{ score }}</div>
              <div class="stat-label">总分</div>
            </div>
            <div class="stat-item">
              <div class="stat-value">{{ correctAnswers }}/{{ totalQuestions }}</div>
              <div class="stat-label">正确率</div>
            </div>
            <div class="stat-item">
              <div class="stat-value">{{ maxCombo }}</div>
              <div class="stat-label">最大连击</div>
            </div>
          </div>
          <div class="result-buttons">
            <el-button type="primary" @click="restartGame" round>
              再来一次
            </el-button>
            <el-button @click="viewLeaderboard" round>
              查看排行榜
            </el-button>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { CircleCheckFilled, CircleCloseFilled, QuestionFilled } from '@element-plus/icons-vue'

// 游戏状态
const gameStarted = ref(false)
const gameFinished = ref(false)
const currentQuestionIndex = ref(0)
const score = ref(0)
const combo = ref(0)
const maxCombo = ref(0)
const correctAnswers = ref(0)
const showAnswer = ref(false)
const selectedAnswer = ref(null)
const timePercentage = ref(100)
let timer = null

// 题目数据
const questions = [
  {
    question: '海洋中最大的塑料污染来源是什么？',
    options: [
      '一次性塑料袋',
      '塑料瓶',
      '渔具',
      '微塑料'
    ],
    correctAnswer: 2,
    explanation: '废弃的渔具（"幽灵渔具"）占据了海洋塑料污染的很大比例，每年造成大量海洋生物死亡。'
  },
  {
    question: '以下哪种海洋生物最容易受到塑料污染的影响？',
    options: [
      '海龟',
      '鲸鱼',
      '海豚',
      '鲨鱼'
    ],
    correctAnswer: 0,
    explanation: '海龟经常将塑料袋误认为水母而误食，导致窒息或消化道堵塞。'
  },
  {
    question: '珊瑚礁白化的主要原因是什么？',
    options: [
      '海水污染',
      '海水温度升高',
      '过度捕捞',
      '海水酸化'
    ],
    correctAnswer: 1,
    explanation: '全球变暖导致海水温度升高，使珊瑚排出共生藻类，从而发生白化现象。'
  }
]

const totalQuestions = questions.length
const currentQuestion = computed(() => questions[currentQuestionIndex.value])
const isCorrect = computed(() => selectedAnswer.value === currentQuestion.value.correctAnswer)

// 开始游戏
const startGame = () => {
  gameStarted.value = true
  startTimer()
}

// 选择答案
const selectAnswer = (index) => {
  selectedAnswer.value = index
  showAnswer.value = true
  clearInterval(timer)
  
  if (isCorrect.value) {
    correctAnswers.value++
    combo.value++
    maxCombo.value = Math.max(maxCombo.value, combo.value)
    score.value += 10 + (combo.value > 1 ? combo.value * 2 : 0)
  } else {
    combo.value = 0
  }

  setTimeout(() => {
    if (currentQuestionIndex.value < totalQuestions - 1) {
      nextQuestion()
    } else {
      gameFinished.value = true
    }
  }, 2000)
}

// 下一题
const nextQuestion = () => {
  currentQuestionIndex.value++
  showAnswer.value = false
  selectedAnswer.value = null
  timePercentage.value = 100
  startTimer()
}

// 计时器
const startTimer = () => {
  const duration = 30 // 30秒
  let timeLeft = duration
  timePercentage.value = 100
  
  timer = setInterval(() => {
    timeLeft--
    timePercentage.value = (timeLeft / duration) * 100
    
    if (timeLeft <= 0) {
      clearInterval(timer)
      selectAnswer(-1) // 超时自动判错
    }
  }, 1000)
}

// 重新开始
const restartGame = () => {
  currentQuestionIndex.value = 0
  score.value = 0
  combo.value = 0
  maxCombo.value = 0
  correctAnswers.value = 0
  gameFinished.value = false
  showAnswer.value = false
  selectedAnswer.value = null
  timePercentage.value = 100
  startTimer()
}

// 查看排行榜
const viewLeaderboard = () => {
  // 实现查看排行榜的逻辑
}
</script>

<style scoped lang="scss">
.game-page {
  .banner {
    height: 200px;
    background: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), 
                url('/public/images/game-banner.jpg') center/cover;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    text-align: center;
    margin-bottom: 30px;
    border-radius: 8px;
    overflow: hidden;

    .banner-content {
      h1 {
        font-size: 2.5em;
        margin-bottom: 15px;
      }
      p {
        font-size: 1.2em;
        opacity: 0.9;
      }
    }
  }

  .game-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 0 20px;
  }

  .start-card {
    .game-intro {
      text-align: center;
      padding: 40px;

      .quiz-icon {
        margin-bottom: 30px;
      }

      h2 {
        font-size: 24px;
        color: #303133;
        margin-bottom: 15px;
      }

      p {
        color: #606266;
        margin-bottom: 30px;
      }

      .rules {
        text-align: left;
        margin: 30px 0;
        padding: 20px;
        background: #f5f7fa;
        border-radius: 8px;

        h3 {
          margin-bottom: 15px;
          color: #303133;
        }

        ul {
          padding-left: 20px;
          
          li {
            margin-bottom: 10px;
            color: #606266;
          }
        }
      }

      .el-button {
        padding: 12px 40px;
        font-size: 16px;
      }
    }
  }

  .question-card {
    .progress-info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
      padding: 20px;
      background: #f5f7fa;
      border-radius: 8px;

      .question-progress {
        display: flex;
        flex-direction: column;
        gap: 5px;

        .progress-text {
          font-size: 16px;
          color: #606266;
        }

        .combo-text {
          color: #e6a23c;
          font-weight: bold;
        }
      }

      .timer-container {
        text-align: center;

        .time-text {
          font-size: 16px;
          font-weight: bold;
          color: #606266;
        }
      }

      .score {
        font-size: 20px;
        font-weight: bold;
        color: #409EFF;
      }
    }

    .question-content {
      margin-bottom: 30px;
      padding: 0 20px;

      h3 {
        font-size: 20px;
        margin-bottom: 30px;
        color: #303133;
        line-height: 1.6;
      }

      .options {
        display: flex;
        flex-direction: column;
        gap: 15px;

        .option-button {
          width: 100%;
          height: auto;
          padding: 12px;
          margin-left: 0;

          .option-wrapper {
            width: 100%;
            display: flex; /* 使用 flexbox 布局 */
            align-items: center;
            flex-direction: column;
            gap: 15px;
          }

          .option-index {
            width: 32px;
            height: 32px;
            line-height: 32px;
            text-align: center;
            background: #f0f2f5;
            border-radius: 50%;
            font-size: 14px;
            flex-shrink: 0;
          }

          .option-text {
            flex-grow: 1;
            text-align: left;
            line-height: 1.5;
            font-size: 14px;
            padding: 4px 0;
          }

          &.correct {
            background-color: #67C23A;
            border-color: #67C23A;
            color: white;

            .option-index {
              background: rgba(255,255,255,0.2);
            }
          }

          &.wrong {
            background-color: #F56C6C;
            border-color: #F56C6C;
            color: white;

            .option-index {
              background: rgba(255,255,255,0.2);
            }
          }
        }
      }
    }

    .feedback {
      margin: 20px;
      padding: 20px;
      border-radius: 8px;
      background: #f5f7fa;

      .feedback-content {
        display: flex;
        align-items: center;
        gap: 10px;
        margin-bottom: 15px;
        font-size: 18px;

        &.correct {
          color: #67C23A;
        }

        &.wrong {
          color: #F56C6C;
        }
      }

      .explanation {
        color: #606266;
        line-height: 1.6;
      }
    }
  }

  .result-card {
    text-align: center;
    padding: 40px;

    h2 {
      font-size: 28px;
      color: #303133;
      margin-bottom: 40px;
    }

    .result-stats {
      display: flex;
      justify-content: space-around;
      margin: 40px 0;

      .stat-item {
        .stat-value {
          font-size: 36px;
          font-weight: bold;
          color: #409EFF;
        }

        .stat-label {
          margin-top: 10px;
          color: #909399;
        }
      }
    }

    .result-buttons {
      display: flex;
      gap: 20px;
      justify-content: center;

      .el-button {
        padding: 12px 30px;
        font-size: 16px;
      }
    }
  }
}

@media (max-width: 768px) {
  .game-page {
    .banner {
      height: 150px;

      h1 {
        font-size: 2em;
      }
    }

    .game-container {
      padding: 0 15px;
    }

    .progress-info {
      flex-direction: column;
      gap: 20px;
      text-align: center;
    }

    .result-stats {
      flex-direction: column;
      gap: 30px;
    }

    .result-buttons {
      flex-direction: column;
      gap: 15px;

      .el-button {
        width: 100%;
      }
    }
  }
}
</style> 